/**
 * Copyright (c) 华南农业大学信息学院蔡超敏2015版权所有
 * 
 * 文件创建时间：2015年4月9日
 */
package org.scauinfo.fpos.action.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.scauinfo.fpos.action.BaseAction;
import org.scauinfo.fpos.model.MemberModel;
import org.scauinfo.fpos.service.MemberService;
import org.scauinfo.fpos.service.MemberpermissionService;
import org.scauinfo.fpos.util.ExceptionUtil;
import org.scauinfo.fpos.util.Json;
import org.scauinfo.fpos.vo.Member;
import org.scauinfo.fpos.vo.Memberpermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

/**
 * @author 蔡超敏
 * 
 */
@Namespace("/")
@Scope("prototype")
@Controller
@Action(value = "member", results = { @Result(name = "member", location = "/member.jsp") })
public class MemberAction extends BaseAction implements ModelDriven<Member> {

	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger.getLogger(MemberAction.class);
	@Autowired
	private MemberService memberService;

	@Autowired
	private MemberpermissionService permissionService;

	public Memberpermission permission = new Memberpermission();
	public Member member = new Member(permission);

	/**
	 * 得到数据列表
	 */
	public void grid() {
		System.out.println("tags ----1");
		int total = memberService.getTotal();
		System.out.println("tags ----total" + total + "page" + page + "rows"
				+ rows + ", sort" + sort + ", order" + order);
		List<Member> list = memberService
				.findAllMember(page, rows, sort, order);
		System.out.println("tags ----2" + list.size());

		List<MemberModel> mList = new ArrayList<MemberModel>();

		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Member member = (Member) iterator.next();
			MemberModel memberModel = new MemberModel(member.getId(),
					member.getMemberId(), member.getName(), member.getGender(),
					member.getTelephone(), member.getAddress());

			if (member.getMemberpermission() != null) {
				memberModel.setMpermissionId(member.getMemberpermission()
						.getMpermissionId());
				memberModel.setMpermissionName(member.getMemberpermission()
						.getMpermissionName());
				memberModel.setMrules(member.getMemberpermission().getMrules());
			}
			mList.add(memberModel);
		}
		writeJson(total, mList);
		System.out.println("tags ----3");
	}

	/**
	 * 添加操作
	 */
	public void add() {
		Json result = new Json();

		System.out.println("tags ----Member" + member.getMemberId());
		System.out.println("tags ----Member"
				+ member.getMemberpermission().getId());
		System.out.println("tags ----Member"
				+ member.getMemberpermission().getMpermissionId());
		try {
			member.setMemberpermission(permissionService.get(member
					.getMemberpermission().getId()));
			System.out.println("tags ----Member"
					+ member.getMemberpermission().getId());

			memberService.saveMember(member);
			result.setSuccess(true);
			result.setMsg("添加顾客成功");
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("添加顾客失败");
		}
		writeJson(result);
	}

	/**
	 * 修改操作
	 */
	public void edit() {
		Json result = new Json();
		try {
			System.out.println("edit ----product" + member.getId());
			System.out.println("edit ----Member"
					+ member.getMemberpermission().getId());
			member.setMemberpermission(permissionService.get(member.getMemberpermission()
					.getId()));
			System.out.println("edit ----Member"
					+ member.getMemberpermission().getId());
			System.out.println("edit ----Member"
					+ member.getMemberpermission().getMpermissionId());
			memberService.updateMember(member);
			result.setSuccess(true);
			result.setMsg("修改顾客成功");
			result.setSuccess(true);
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("修改顾客失败");
		}
		writeJson(result);
	}

	/**
	 * 删除操作
	 */
	public void remove() {
		Json result = new Json();
		try {
			System.out.println("tags ----=" + member.getId());
			memberService.deleteMember(member.getId());
			result.setSuccess(true);
			result.setMsg("删除顾客成功");
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("删除顾客失败");
		}
		writeJson(result);
	}

	@Override
	public Member getModel() {
		return member;
	}
}